/****************************************************************************/    
/* NAME:           Hal_InputManagement.c                                    */    
/* PURPOSE:        Manage all basic input and output                        */    
/* CREATED_BY:     Vyx                                                      */    
/* CREATION_DATE:  03/07/2012                                               */ 
/************************************** (C) Copyright 2012 AquaLink *********/  

#include "Hal_InputManagement.h"
#include "Hal_InputManagement_Config.h"
#include "stdio.h"

#define INPUT_NUMBER         (sizeof(InputConfigList) / sizeof(InputConfigList[0]))
#define INPUT_TASK_DELAY     (10) /* 10ms */ 
#define INPUT_SAMPLE_NUMBER  (3)

bool InputFiltered[INPUT_NUMBER];

/**    
  * @brief  Main function of input management.  
  *         This function is execute one time at the start.
  * @param  none    
  * @retval None    
  */  
void vTaskCTRL_InputManagementInit(void)
{  
  GPIO_InitTypeDef GPIO_InitStructure;
  uint8_t          i;
  
  /* init all input pin */
  for (i=0; i<INPUT_NUMBER; i++)
  {
    /* start clock gpio if is needed */
    if (InputConfigList[i].InputGpio == GPIO_A)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOA ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_B)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOB ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_C)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOC ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_D)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOD ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_E)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOE ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_F)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOF ,ENABLE); 
    }
    if (InputConfigList[i].InputGpio == GPIO_G)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOG ,ENABLE); 
    }
  
    GPIO_InitStructure.GPIO_Pin  = InputConfigList[i].InputPin;      
    GPIO_InitStructure.GPIO_Mode = InputConfigList[i].InputMode;      
    GPIO_Init(InputConfigList[i].InputGpio, &GPIO_InitStructure);  
  }
}

/**    
  * @brief  Service to get filtered input  
  * @param  none    
  * @retval None    
  */ 
void vTaskGET_InputFilteredManagement()
{  
  portTickType   xLastWakeTime;
  static bool    InputHistory[INPUT_NUMBER][INPUT_SAMPLE_NUMBER];
  uint8_t        SampleID;
  uint8_t        InputNb;
  
  /* init */
  xLastWakeTime = xTaskGetTickCount();
  SampleID = 0;
  printf("point 0\r\n");
  
  while(1)
  {
    printf("point 1\r\n");
  
    for (InputNb=0; InputNb<INPUT_NUMBER; InputNb++)
    {
      InputHistory[InputNb][SampleID] = INPUT_GetInputValue(InputNb);
    }

    printf("point 2\r\n");
    
    if (SampleID < INPUT_SAMPLE_NUMBER)
    {
      SampleID++;
      printf("point 3\r\n");
    }
    else
    {
      for (InputNb=0; InputNb<INPUT_NUMBER; InputNb++)
      {
        for(SampleID=0; SampleID<(INPUT_SAMPLE_NUMBER-1); SampleID++)
        {
          printf("point 4\r\n");
        
          if ((InputHistory[InputNb][SampleID]) != (InputHistory[InputNb][SampleID+1]))
          {
            InputFiltered[InputNb] = FALSE;
            break;
          }
          else
          {
            InputFiltered[InputNb] = TRUE;
          }
        }
      }
      SampleID = 0;
    }

    printf("point 5\r\n");
   
    wait(INPUT_TASK_DELAY);
  }
}

/**    
  * @brief  Get the value of selected input.    
  * @param  Name of input 
  * @retval Value of input (TRUE or FALSE)
  */  
bool INPUT_GetInputValue(uint8_t InputName)
{
  return GPIO_ReadInputDataBit(InputConfigList[InputName].InputGpio,
                                InputConfigList[InputName].InputPin);
}

/**    
  * @brief  Get the value of filtered selected input.    
  * @param  Name of input 
  * @retval Value of input (TRUE or FALSE)
  */  
bool INPUT_GetFilteredInputValue(uint8_t InputName)
{
  return InputFiltered[InputName];
}
